package edu.ncu.controller;

import edu.ncu.dao.UserDao;
import edu.ncu.entity.*;
import edu.ncu.service.CommentService;
import edu.ncu.service.PhotoService;
import edu.ncu.service.PublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService ;
    @Autowired
    private PublishService publishService ;
    @Autowired
    private UserDao userDao ;
    @Autowired
    private PhotoService photoService ;

    @GetMapping("/comment/{commentId}/detail")
    public String comment_detail(@PathVariable int commentId, Model model) {
        Comment comment = commentService.findCommentById(CommentEnum.PUBLISH,commentId) ;
        // 查询comment对应的所有子评论
        model.addAttribute("comment",comment) ;
        return "comment_detail" ;
    }

    /**
     * 评论某个动态
     * @param pId
     * @param publish_comment
     * @param request
     * @param model
     * @return
     */
    @PostMapping("/publish/{pId}/comment")
    public String comment_edit(
            @PathVariable int pId,
            String publish_comment,
            HttpServletRequest request,Model model){

        Publish publish = publishService.getPublishById(pId) ;
        publishService.updatePublish(pId) ; // 数据库中评论数+1

        publish.setCommentNum(publish.getCommentNum()+1); // 评论数+1

        // 当前的user表示评论的用户
        User user = (User) request.getSession().getAttribute("user") ;
        // 测试数据
        // User user = userDao.findByUserId(2) ;

        List<Comment> comments = publish.getComments() ;

        Comment comment = new Comment() ;
        comment.setContent(publish_comment);

        comment.setCommenterId(user.getUserId());
        comment.setUser(user);
        comment.setCommentTime(new Date());
        commentService.saveComment(CommentEnum.PUBLISH,comment) ;
        // 方便查询
        comments.add(comment) ;
        model.addAttribute("publish",publish) ;
        return "publish_detail" ;
    }

    /**
     * @{/comment/{id}/comment(id=${comment.commentId})}
     * id 表示父评论的id
     * pId 表示动态的id
     * @return
     */
    @PostMapping("/pubcomment/{id}/comment/{pId}")
    public String commentToCommentOfPublish(@PathVariable int id,@PathVariable int pId, HttpServletRequest request,String comment_content,Model model) {
        User user = (User) request.getSession().getAttribute("user") ;
        Comment comment = commentService.findCommentById(CommentEnum.PUBLISH,id) ; // 找到父评论
        // 更新父评论的评论数+1
        commentService.updateCommentNum(CommentEnum.PUBLISH,id) ;
        // 新增子评论
        Comment child = new Comment() ;
        child.setUser(user);
        child.setCommentTime(new Date());
        child.setContent(comment_content);

        child.setCommenterId(user.getUserId());
        commentService.saveComment(CommentEnum.PUBLISH,child) ;

        // 将新增的评论加到list里面，方便遍历, 获取夫评论的子评论
        List<Comment> comments = comment.getComments() ;
        if (comments == null) {
            comments = new ArrayList<>() ;
        }
        comments.add(child) ;

        Publish publish = publishService.getPublishById(pId) ;
        // 接着应该将动态的comment
        // publish.setComments(comments);
        model.addAttribute("publish",publish) ;
        return "publish_detail" ;
    }

    /**
     * @{/comment/{id}/comment(id=${comment.commentId})}
     * id 表示父评论的id
     * pId 表示照片的id
     * @return
     */
    @PostMapping("/pho_comment/{id}/comment/{photoId}")
    public String commentToCommentOfPhoto(@PathVariable int id,@PathVariable int photoId, HttpServletRequest request,String comment_content,Model model) {
        User user = (User) request.getSession().getAttribute("user") ;
        Comment comment = commentService.findCommentById(CommentEnum.PHOTO,id) ; // 找到父评论
        // 更新父评论的评论数+1
        commentService.updateCommentNum(CommentEnum.PHOTO,id) ;
        // 新增子评论
        Comment child = new Comment() ;
        child.setUser(user);
        child.setCommentTime(new Date());
        child.setContent(comment_content);

        child.setCommenterId(user.getUserId());
        commentService.saveComment(CommentEnum.PHOTO,comment) ;

        // 将新增的评论加到list里面，方便遍历
        List<Comment> comments = comment.getComments() ;
        comments.add(child) ;

        Photo p = photoService.findPhotoById(photoId) ;
        p.setComments(comments);
        model.addAttribute("photo",p) ;
        return "photos" ;
    }
}
